package com.ueboot.weishop.service.order.impl;

import com.google.common.collect.Lists;
import com.ueboot.weishop.entity.order.OrderEntity;
import com.ueboot.weishop.repository.order.OrderNewRepository;
import com.ueboot.weishop.service.order.OrderNewService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrderNewServiceImpl implements OrderNewService {

	@Autowired
	private OrderNewRepository orderNewRepository;

	@Override
	public List<OrderEntity> findSearch(OrderEntity model, Date payTimeStart, Date payTimeEnd) {
		Assert.notNull(model);
		List<OrderEntity> orderEntityList = orderNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
			List<Predicate> predicateList = this.commonPredicates(root, criteriaBuilder, model, payTimeStart, payTimeEnd);
			Predicate[] predicates = new Predicate[predicateList.size()];
			return criteriaBuilder.and(predicateList.toArray(predicates));
		});
		return orderEntityList;
	}

	@Override
	public Page<OrderEntity> pagingSearch(Pageable pageable, OrderEntity model, Date payTimeStart, Date payTimeEnd) {
		Assert.notNull(model);
		Page<OrderEntity> pageDataOrder = orderNewRepository.findAll((root, query, criteriaBuilder) -> {
			List<Predicate> predicateList = this.commonPredicates(root, criteriaBuilder, model, payTimeStart, payTimeEnd);
			Predicate[] predicates = new Predicate[predicateList.size()];
			return criteriaBuilder.and(predicateList.toArray(predicates));
		}, pageable);
		return pageDataOrder;
	}

	private List<Predicate> commonPredicates(Root<OrderEntity> root, CriteriaBuilder criteriaBuilder, OrderEntity model, Date payTimeStart, Date payTimeEnd) {
		List<Predicate> predicateList = Lists.newLinkedList();

		if (StringUtils.isNotEmpty(model.getGoodsName())) {
			predicateList.add(criteriaBuilder.like(root.get("goodsName").as(String.class), "%" + model.getGoodsName() + "%"));
		}

		if (StringUtils.isNotEmpty(model.getOrderNo())) {
			predicateList.add(criteriaBuilder.equal(root.get("orderNo").as(String.class), model.getOrderNo()));
		}

		if (StringUtils.isNotEmpty(model.getOrderStatus())) {
			predicateList.add(criteriaBuilder.equal(root.get("orderStatus").as(String.class), model.getOrderStatus()));
		}

		if (StringUtils.isNotEmpty(model.getRefundStatus())) {
			predicateList.add(criteriaBuilder.equal(root.get("refundStatus").as(String.class), model.getRefundStatus()));
		}
		if (payTimeStart != null) {
			predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("payTime").as(Date.class), payTimeStart));
		}

		if (payTimeEnd != null) {
			predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("payTime").as(Date.class), payTimeEnd));
		}
		return predicateList;
	}
}
